﻿
@{
    ViewData["Title"] = "UptWarehouse";
    Layout = "~/Views/Shared/Temp.cshtml";
}


<blockquote class="layui-elem-quote">
    <h2 style="color:darkgray">编辑仓库</h2>
</blockquote>

<div class="layui-card" style="width:80%;margin:auto;">
    <div class="layui-card-header">
        <table style="border:1px solid darkgray;width:100%;height:50px;margin-top:30px">
            <tr>
                <td style="color:darkgray">编辑仓库信息</td>
            </tr>
        </table>
    </div>
    <div class="layui-card-body">
        <form class="layui-form layui-row layui-col-space16">
            <div class="layui-row" style="margin-bottom: 20px;">
                <div class="layui-col-xs6">
                    <label class="layui-form-label">仓库编号</label>
                    <div class="layui-input-block">
                        <input type="text" id="warehouseCode" lay-verify="WarehouseCode" placeholder="请输入" autocomplete="off" class="layui-input" style="width:150px" disabled>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-block" style="width:150px">
                        <select id="department" lay-filter="部门" lay-verify="Department">
                            <option value=""></option>
                            <option value=1>财务中心</option>
                            <option value=2>信息技术中心</option>
                            <option value=3>采购中心</option>
                            <option value=4>销售中心</option>
                            <option value=5>行政部</option>
                            <option value=6>管理中心</option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-row" style="margin-bottom: 20px;">
                <div class="layui-col-xs6">
                    <label class="layui-form-label">仓库名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="warehouseName" lay-verify="WarehouseName" placeholder="请输入"  class="layui-input" style="width:220px">
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">仓库类型</label>
                    <div class="layui-input-block" style="width:220px">
                        <select id="warehouseType" lay-filter="aihao" lay-verify="WarehouseType">
                            <option value=""></option>
                            <option value="产品仓库">产品仓库</option>
                            <option value="原料仓库">原料仓库</option>
                            <option value="办成品仓库">办成品仓库</option>
                            <option value="废品仓库">废品仓库</option>
                        </select>
                    </div>
                </div>
            </div>


            <div class="layui-row" style="margin-bottom: 20px;">
              
                <div class="layui-col-xs6">
                    <label class="layui-form-label">租赁日期</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-date"></i>
                        </div>
                        <input type="date" name="RentalTime" id="rentalTime" lay-verify="RentalTime" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                    </div>

                </div>
            </div>

            <div class="layui-row" style="margin-bottom: 20px;">
                <div class="layui-col-sm6">
                    <label class="layui-form-label">面积</label>
                    <div class="layui-input-block">
                        <input type="text" id="area" lay-verify="Area" placeholder="请输入" autocomplete="off" class="layui-input" style="width:220px">
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">地址</label>
                    <div class="layui-input-block">
                        <input type="text" id="waddress" lay-verify="Waddress" placeholder="请输入" autocomplete="off" class="layui-input" style="width:220px">
                    </div>
                </div>
            </div>

            <div class="layui-row" style="margin-bottom: 20px;">
                <div class="layui-col-xs6">
                    <label class="layui-form-label">联系人</label>
                    <div class="layui-input-block">
                        <input type="text" id="wcontact" lay-verify="Wcontact" placeholder="请输入" autocomplete="off" class="layui-input" style="width:220px">
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-block">
                        <input type="text" id="wphone" lay-verify="Wphone" placeholder="请输入" autocomplete="off" class="layui-input" style="width:220px">
                    </div>
                </div>
            </div>
            <div style="text-align:center;">
                <input type="button" class="layui-btn layui-bg-blue" value="提交" lay-submit lay-filter="demo-verify">
                <input type="button" value="取消" class="layui-btn-primary layui-btn">
            </div>

        </form>
    </div>
</div>






<script>
    layui.use( function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        //获取跳转过来的id
        var urlParams = new URLSearchParams(window.location.search);
        var id = urlParams.get('id');
        console.log(id);


        var token = localStorage.getItem("token");

            form.verify({
                WarehouseCode: function (value, elem) {

                    if (!value) {
                        return "请输入仓库编号"
                    } ;

                },
                WarehouseName: function (value, elem) {
                    if (!value) {
                        return "请输入仓库名称"
                    }
                },
                Department: function (value, elem) {
                    if (!value) {
                        return "请选择部门"
                    }
                },
                    WarehouseType: function (value, elem) {
                    if (!value) {
                        return "请选择仓库类型"
                    }
                },
                Function: function (value, elem) {
                    if (!value) {
                        return "请输入仓库作用"
                    }

                },
                RentalTime: function (value, elem) {
                    if (!value) {
    return "请选择租赁日期"
                    }
                },
                Area: function (value, elem) {
                   if (!value) {
                        return "请输入仓库面积"
                    }
                },
                Waddress: function (value, elem) {
                    if (!value) {
                        return "请输入仓库地址"
                    }
                },
                Wcontact: function (value, elem) {
                    if (!value) {
                        return "请输入联系人"
                    }
                },
                Wphone: function (value, elem) {
                     if (!value) {
                        return "请输入联系电话"
                     }
                }
            });

            form.on('submit(demo-verify)', function (data) {
                console.log(data);
                var field = data.field; // 获取表单字段值
                // 显示填写结果，仅作演示用
                      console.log(field);
                layer.alert(JSON.stringify(field), {
                    title: '当前填写的字段值'
                });

                return false; // 阻止默认 form 跳转
            });


      
        var data = {
            Id: id,
            Token: token
        }
         //反填
            $.ajax({
               
                url: '/Foundation/GetWareInfo',
                type: 'get',
                data: data,
                success: function (res) {
                // debugger;
              
                    var resdata = JSON.parse(res);
                    console.log(resdata.data);
                    var data = resdata.data;
                      var date = new Date(resdata.data.rentalTime);
                      // 获取年、月、日
                     var year = date.getFullYear();
                     var month = (date.getMonth() + 1).toString().padStart(2, '0');  // 月份从0开始，因此加1
                     var day = date.getDate().toString().padStart(2, '0');  // 补充两位数字
                      // 拼接成 "yyyy-MM-dd" 格式
                      var formattedDate = `${year}-${month}-${day}`;
                      console.log(formattedDate);
                $("#warehouseCode").val(data.warehouseCode);
                $("#warehouseName").val(data.warehouseName);
                $("#department").val(data.department);
                    layui.form.render('select');
                $("#warehouseType").val(data.warehouseType);
                    layui.form.render('select');
                $("#function").val(data.function);
                $("#rentalTime").val(formattedDate);
                $("#area").val(data.area);
                $("#waddress").val(data.waddress);
                $("#wcontact").val(data.wcontact);
                $("#wphone").val(data.wphone);
                }
            })


            //修改仓库信息
        form.on('submit(demo-verify)', function (data) {
            // console.log(data);
            // var field = data.field; // 获取表单字段值
            // 显示填写结果，仅作演示用
            var WarehouseCode = $("#warehouseCode").val();
            var WarehouseName = $("#warehouseName").val();
            var Department = $("#department").val();
            var WarehouseType = $("#warehouseType").val();
            var Function = $("#function").val();
            var RentalTime = $("#rentalTime").val();
            var Area = $("#area").val();
            var Waddress = $("#waddress").val();
            var Wcontact = $("#wcontact").val();
            var Wphone = $("#wphone").val();
            var field = {
                warehouseId: id,
                warehouseCode: WarehouseCode,
                warehouseName: WarehouseName,
                department: Department,
                warehouseType: WarehouseType,
                function: Function,
                rentalTime: RentalTime,
                area: Area,
                waddress: Waddress,
                wcontact: Wcontact,
                wphone: Wphone,
                Token: token
            }

            console.log(field);
            //return false; // 阻止默认 form 跳转
            $.ajax({
                url: '/Foundation/UpdateWarehouseInfo',
                type: 'put',
                data: field,
                success: function (res) {
                    var s = JSON.parse(res);
                    console.log(s);
                   if (s.data > 0) {
                        layer.msg('修改成功', { icon: 1 }, function () {
                            location.href = '/Foundation/GetWarehouse';
                            
                            
                        });

                    }else {

                    }
                }
            })
          
        })

            })



</script>

